using System.Collections;
using System.Collections.Generic;
using UnityEngine;
using System;

public class ObjectTestSceneMgr : BaseScene
{
    // Start is called before the first frame update
    void Start()
    {
        AssetBundleManager abmgr = AssetBundleManager.Instance;

        UIManager.Instance.OpenPlane<HotUpdatePanel>();

        ObjectPoolManager.Instance.InitBundles(abmgr.bundles[abmgr.POOL_BUNDLE], abmgr.bundles);

        SoundManager.Instance.Init();

        SoundManager.Instance.Play("music_01", SoundStyle.backGround);
    }

    // Update is called once per frame
    void Update()
    {

    }
}
public class SoundTestSceneMgr : BaseScene
{
    // Start is called before the first frame update
    void Start()
    {
        AssetBundleManager abmgr = AssetBundleManager.Instance;

        ObjectPoolManager.Instance.InitBundles(abmgr.bundles[abmgr.POOL_BUNDLE], abmgr.bundles);

        SoundManager.Instance.Init();

        SoundManager.Instance.Play("music_01", SoundStyle.backGround);

        SoundManager.Instance.SetLoop(SoundStyle.backGround, true);

        SoundManager.Instance.SoundTrack[SoundStyle.backGround].Nodes.First.Value.onPlaying = Palying;

    }

    // Update is called once per frame
    void Update()
    {
        if (Input.GetKey(KeyCode.I))
        {
            SoundManager.Instance.SetVolum(SoundManager.Instance.SoundTrack[SoundStyle.backGround].Volum + Time.deltaTime, SoundStyle.backGround);
        }
        if (Input.GetKey(KeyCode.O))
        {
            SoundManager.Instance.SetVolum(SoundManager.Instance.SoundTrack[SoundStyle.backGround].Volum - Time.deltaTime, SoundStyle.backGround);
        }
        if (Input.GetKeyDown(KeyCode.P))
        {
            SoundManager.Instance.Pause(SoundStyle.backGround);
        }
        if (Input.GetKeyDown(KeyCode.L))
        {
            SoundManager.Instance.SetLoop(SoundStyle.backGround, !SoundManager.Instance.SoundTrack[SoundStyle.backGround].IsLoop);
        }
        if (Input.GetKeyDown(KeyCode.M))
        {
            SoundManager.Instance.SetMute(SoundStyle.backGround, !SoundManager.Instance.SoundTrack[SoundStyle.backGround].IsMute);
        }
    }

    float[] samples = new float[128];
    float result;
    float rate = 1f / 12f;
    void Palying(SoundPlayerNode node)
    {
        node.audioSource.GetOutputData(samples, 0);
        result = 0;
        for (int i = 0; i < samples.Length; i++)
        {
            result += samples[i];
        }
        result *= rate;
        ObjectPoolManager.Instance.DePool<Cube>("Cube").transform.localScale = new Vector3(0.5f, 0.5f, result);
    }
}

public class EventTestSceneMgr : BaseScene
{
    int hash;
    Action action;
    void Start()
    {
        hash = typeof(EventTestSceneMgr).GetHashCode();
        EventManager.Instance.Init();
        action = () => { Debug.Log("Test"); };
    }

    private void Update()
    {
        if (Input.GetKeyDown(KeyCode.Q))
        {
            EventManager.Instance.AddListening(hash,action);
        }

        if (Input.GetKeyDown(KeyCode.E))
        {
            EventManager.Instance.RemoveListening(hash, action);
        }

        if (Input.GetKeyDown(KeyCode.T))
        {
            EventManager.Instance.TriggerEvent(hash);
        }
    }


}

public class Cube : BasePoolElement
{


    Vector3 dir;
    float time;
    float lifeTime;
    public override void PoolInit()
    {
        transform.position = Vector3.right * -3;
        time = 0;
        lifeTime = 6;
        dir = Vector3.right;
    }
    private void FixedUpdate()
    {
        time += Time.fixedDeltaTime;
        if (time > lifeTime)
        {
            ObjectPoolManager.Instance.EnPool(this);
        }
        transform.position += dir * Time.deltaTime;
    }
}